package com.sdp.wx.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wx.db.wx_metering_utensil.entity.WxMeteringUtensil;
import com.sdp.wx.db.wx_metering_utensil.service.IWxMeteringUtensilService;
import com.sdp.wx.db.wx_metering_utensil_record.entity.WxMeteringUtensilRecord;
import com.sdp.wx.db.wx_metering_utensil_record.service.IWxMeteringUtensilRecordService;
import com.sdp.wx.utils.CurrentLoginEmp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-02-26
 */
@RestController
@RequestMapping("/WxMeteringUtensilController")
public class WxMeteringUtensilController {
    @Autowired
    private Query query;
    @Autowired
    private CurrentLoginEmp currentLoginEmp;
    @Autowired
    private IWxMeteringUtensilService iWxMeteringUtensilService;
    @Autowired
    private IWxMeteringUtensilRecordService iWxMeteringUtensilRecordService;
    @PostMapping(value = "/page")
    public Result<?> page(@RequestBody DataCenter dataCenter) {
        return Result.ok(query.page(iWxMeteringUtensilService, WxMeteringUtensil.class, dataCenter));
    }

    @PostMapping(value = "/saveWxMeteringUtensil")
    @Transactional(rollbackFor=Exception.class)
    public Result<?> save(@RequestBody WxMeteringUtensil wxMeteringUtensil) {
        if(wxMeteringUtensil == null) {
            return Result.error("提交信息为空");
        }
        JSONObject emp = currentLoginEmp.getCurrentLoginInfo();
        if(StringUtils.isEmpty(wxMeteringUtensil.getKeyId())) {
            wxMeteringUtensil.setOrgNo(emp.get("orgNo").toString());
            iWxMeteringUtensilService.save(wxMeteringUtensil);
        } else {
            iWxMeteringUtensilService.updateById(wxMeteringUtensil);
        }
        return Result.ok(wxMeteringUtensil);
    }

    @PostMapping(value = "/save")
    public Result<?> page(@RequestBody JSONObject data) {
        WxMeteringUtensil wxEmpWorkAttendance = data.getObject("formData", WxMeteringUtensil.class);
        List<WxMeteringUtensilRecord> recordList = JSONObject.parseArray(JSON.toJSONString(data.get("recordList")), WxMeteringUtensilRecord.class);
        Collections.sort(recordList, new Comparator<WxMeteringUtensilRecord>() {
            @Override
            public int compare(WxMeteringUtensilRecord o1, WxMeteringUtensilRecord o2) {
                return o2.getIdentifyTime().compareTo(o1.getIdentifyTime());
            }
        });
        wxEmpWorkAttendance.setIdentifyTime(recordList.get(0).getIdentifyTime());
        if(StringUtils.isEmpty(wxEmpWorkAttendance.getKeyId())){
            iWxMeteringUtensilService.save(wxEmpWorkAttendance);
        }else{
            iWxMeteringUtensilService.updateById(wxEmpWorkAttendance);
        }
        // 删除旧数据
        QueryWrapper<WxMeteringUtensilRecord> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(WxMeteringUtensilRecord::getDeviceId,wxEmpWorkAttendance.getKeyId());
        iWxMeteringUtensilRecordService.remove(queryWrapper);
        // 保存新数据
        for(WxMeteringUtensilRecord wxMeteringUtensilRecord:recordList){
            wxMeteringUtensilRecord.setDeviceId(wxEmpWorkAttendance.getKeyId());
        }
        iWxMeteringUtensilRecordService.saveBatch(recordList);
        return Result.ok();
    }
}

